//
//  CaptureCaseView.swift
//  SwiftuiUno
//
//  Created by baozi on 2023/11/27.
//

import SwiftUI


struct CaptureCaseView: View {
    @StateObject var camera = CaptureLogic()
    @State var showAlbum: Bool = false
    @State var selectedImgIdx: Int = 0
    @EnvironmentObject var router: Router
    var body: some View{
        ZStack {
            CameraPreview(camera: camera)
                .ignoresSafeArea()
            VStack {
                HStack {
                    Spacer()
                    
                    Button(action: {
                        router.to(name: .editCase(camera: camera))
                    }, label: {
                        Image(systemName: "slider.horizontal.3")
                            .foregroundColor(.black)
                            .padding()
                            .background(Color.white)
                            .clipShape(Circle())
                    })
                    .padding(.trailing,10)
                }
                
                Spacer()
                VStack {
                    HStack {
                        ForEach(camera.picDatas) { data in
                            if let image = UIImage(data: data.datas) {
                                Image(uiImage: image)
                                    .resizable()
                                    .aspectRatio(contentMode: .fill)
                                    .frame(width: 50, height: 50)
                                    .cornerRadius(12)
                                    .onTapGesture {
                                        if let idx = camera.picDatas.firstIndex(where: {$0.id == data.id}) {
                                            selectedImgIdx = idx
                                        }
                                        showAlbum.toggle()
                                    }
                                    .contextMenu {
                                        Button {
                                            withAnimation {
                                                camera.picDatas.removeAll { rdata in
                                                    rdata.id == data.id
                                                }
                                            }
                                        } label: {
                                            HStack {
                                                Text("删除")
                                                    .foregroundColor(.red)
                                                Image(systemName: "trash")
                                                    .resizable()
                                            }
                                        }
                                    }
                                    .animation(.easeInOut, value: true)
                            }
                        }
                    }
                    HStack{
                        if false {
                            Button(action: { if !camera.isSaved{camera.savePic()}}, label: {
                                Text(camera.isSaved ? "Saved":"Save")
                                    .foregroundColor(.black)
                                    .fontWeight(.semibold)
                                    .padding(.vertical,10)
                                    .padding(.horizontal,20)
                                    .background(Color.white)
                                    .clipShape(Capsule())
                            })
                            .padding(.leading)
                            
                            Spacer()
                            
                        } else {
                            Button(action: camera.takePic, label: {
                                ZStack{
                                    
                                    Circle()
                                        .fill(Color.white)
                                        .frame(width: 65, height: 65, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                                    Circle()
                                        .stroke(Color.white,lineWidth: 2)
                                        .frame(width: 75, height: 75, alignment: .center)
                                    
                                    
                                }
                            })
                        }
                    }
                    .frame(height:75)
                }
            }
        }
        .fullScreenCover(isPresented: $showAlbum, content: {
            PhotoAlbumPreView(selectedImgIdx: $selectedImgIdx, picDatas: $camera.picDatas)
        })
        .onAppear(perform: {
            camera.Check()
        })
        
    }
}

#Preview {
    ZStack {
        Color.gray.ignoresSafeArea()
        CaptureCaseView()
    }
    
}
